//Erstellt von Moritz Liepe
package main.editor;

import main.TextControl;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class EditModuleDialog extends Dialog {

    private Label dialogLabel;
    private Text dialogText;
    private TextControl textControl;
    
    protected EditModuleDialog(Shell parentShell) {
        super(parentShell);
    }
    
    public EditModuleDialog(Shell parentShell, TextControl textControl) {
        super(parentShell);
        this.textControl = textControl;
    }
    
    @Override
    protected void configureShell(Shell shell){
    	super.configureShell(shell);
    	shell.setText("Edit Module");
    	shell.setSize(400, 300);
    }
    
    @Override
    public void okPressed() {
       
    	textControl.setValue(dialogLabel.getText() + dialogText.getText());
    	
    	close();
    }
    
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite)super.createDialogArea(parent);
        GridLayout grd = new GridLayout(2, false);
        grd.marginWidth = 5;
        grd.verticalSpacing = 5;
        composite.setLayout(grd);
        
        dialogLabel = new Label(composite, SWT.LEFT);
        if (textControl.getType().equalsIgnoreCase("fileinput")) dialogLabel.setText("File:");
        else dialogLabel.setText("");
        dialogText = new Text(composite,SWT.LEFT | SWT.FLAT | SWT.BORDER);
        dialogText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        dialogText.setText(textControl.getValue());
        
        return composite;
    }
    
    @Override
    protected void initializeBounds()
    {
	    super.initializeBounds();
	    Shell shell = this.getShell();
	    Monitor primary = shell.getMonitor();
	    Rectangle bounds = primary.getBounds ();
	    Rectangle rect = shell.getBounds ();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation (x, y);
    } 
}
